www.gusucode.com > 基于matlab编程空间复用空时码的仿真程序源码 > 基于matlab编程空间复用空时码的仿真程序源码/2Tx,2Rx,Spatial Multiplex/dem_DFUSC_2x2_SM.m

    function [ser,mse]=dem_DFUSC_2x2_SM(demparams,NoisePwr,ipfid_rx1,ipfid_rx2)

PwNorm=sqrt(2048/1702);     % symbol power normalization factor
% load original data
switch demparams.qam,
    case 4,
        load orig_DFUSC_QPSK.mat;
        ModScheme='QPSK';
    case 16,
        load orig_DFUSC_QAM16.mat;
        ModScheme='16QAM';
    case 64,
        load orig_DFUSC_QAM64.mat;
        ModScheme='64QAM';
    otherwise,
        error('Not supported Modulation Schme!');
end
load Wk_preamble.mat;

    % get preamble modulation and index values
    preamble_index_tx1=1:2:1702;    % Tx 1 use even subcarriers
    preamble_index_tx2=0:2:1702;    % Tx 2 use odd subcarriers
    preamble_mod_tx1=sqrt(2)*sqrt(2)*2*(0.5-Wk_preamble(2:2:end));  % Note: include DC. At No.426 element
    preamble_mod_tx2=sqrt(2)*sqrt(2)*2*(0.5-Wk_preamble(1:2:end));

    % ######## Perfect Channel Estimation ########
        switch demparams.chantype,
        case 1,
            load perfectCE_sui1_11.mat;
            load perfectCE_sui1_21.mat;
            load perfectCE_sui1_12.mat;
            load perfectCE_sui1_22.mat;
        case 2,
            load perfectCE_sui2_11.mat;
            load perfectCE_sui2_21.mat;
            load perfectCE_sui2_12.mat;
            load perfectCE_sui2_22.mat;
        case 3,
            load perfectCE_sui3_11.mat;
            load perfectCE_sui3_21.mat;
            load perfectCE_sui3_12.mat;
            load perfectCE_sui3_22.mat;
        case 4,
            load perfectCE_sui4_11.mat;
            load perfectCE_sui4_21.mat;
            load perfectCE_sui4_12.mat;
            load perfectCE_sui4_22.mat;
        case 5,
            load perfectCE_sui5_11.mat;
            load perfectCE_sui5_21.mat;
            load perfectCE_sui5_12.mat;
            load perfectCE_sui5_22.mat;
        case 6,
            load perfectCE_sui6_11.mat;
            load perfectCE_sui6_21.mat;
            load perfectCE_sui6_12.mat;
            load perfectCE_sui6_22.mat;
        otherwise,
            error('Unknown SUI channel type!');
    end
    perfectCE_11(:,1877:2048)=[]; perfectCE_11(:,1:173)=[];
    perfectCE_21(:,1877:2048)=[]; perfectCE_21(:,1:173)=[];
    perfectCE_12(:,1877:2048)=[]; perfectCE_12(:,1:173)=[];
    perfectCE_22(:,1877:2048)=[]; perfectCE_22(:,1:173)=[];
    % ################################################################
    
suberr=0;   % used to calculate subcarrier error rate
mse=0;
datanum=demparams.numframe*2*demparams.sympf*1702;  % data number in one frame of 2 Tx

for frm=1:demparams.numframe
    % ############## When use Known Channel Knowledge #############
    %FullChanEst_11=sqrt(2048/1702)*perfectCE_11(frm,:);
    %FullChanEst_21=sqrt(2048/1702)*perfectCE_21(frm,:);
    %FullChanEst_12=sqrt(2048/1702)*perfectCE_12(frm,:);
    %FullChanEst_22=sqrt(2048/1702)*perfectCE_22(frm,:);
    % #############################################################
    
    % use preamble for channel estimation
       %---------- Path 11 and 21-------------
    [preamble_time_rx1, symlen] = fread(ipfid_rx1, 2*2048*(1+demparams.Guard), 'float32');
    if symlen~=2*2048*(1+demparams.Guard), error('fread Rx 1 input data error!'); end
    preamble_time_rx1 =  [ 1 1j ] * reshape(preamble_time_rx1, 2, symlen/2);
    symlen=symlen/2;
    % remove CP
    preamble_time_rx1=preamble_time_rx1((2048*demparams.Guard+1):end);
            % ###### Time domain ls estimation ######
            [FullChanEst_11,FullChanEst_21]=ls_time_2x2((preamble_time_rx1).',demparams.chantype,demparams.sampletime);
            FullChanEst_11=PwNorm*FullChanEst_11; FullChanEst_21=PwNorm*FullChanEst_21;
            % #######################################
    % FFT
%    preamble_freq_rx1=fftshift(fft(preamble_time_rx1,2048)/sqrt(2048));
    % Trim Guard subcarriers
%    preamble_freq_rx1(1877:2048)=[];  preamble_freq_rx1(1:173)=[];
            % Extract preamble pilots AND Channel Estimation
%            preamble_pilots_tx1=preamble_freq_rx1(preamble_index_tx1+1);
%            preamble_pilots_tx2=preamble_freq_rx1(preamble_index_tx2+1);
%            ChanEst_11=ls_freq(preamble_pilots_tx1,preamble_mod_tx1);
%            ChanEst_21=ls_freq(preamble_pilots_tx2,preamble_mod_tx2);
            % Interpolate  Note: If interpolate 'Tx1', must remove DC
%            FullChanEst_11=interpolate_2x2(preamble_index_tx1+1,ChanEst_11,'linear','Tx1');
%            FullChanEst_21=interpolate_2x2(preamble_index_tx2+1,ChanEst_21,'linear','Tx2');
            
       %------------ Pathe 12 and 22-----------
    [preamble_time_rx2, symlen] = fread(ipfid_rx2, 2*2048*(1+demparams.Guard), 'float32');
    if symlen~=2*2048*(1+demparams.Guard), error('fread Rx 2 input data error!'); end
    preamble_time_rx2 =  [ 1 1j ] * reshape(preamble_time_rx2, 2, symlen/2);
    symlen=symlen/2;
    % remove CP
    preamble_time_rx2=preamble_time_rx2((2048*demparams.Guard+1):end);
            % ###### Time domain ls estimation ######
            [FullChanEst_12,FullChanEst_22]=ls_time_2x2((preamble_time_rx2).',demparams.chantype,demparams.sampletime);
            FullChanEst_12=PwNorm*FullChanEst_12; FullChanEst_22=PwNorm*FullChanEst_22;
            % #######################################
    % FFT
 %   preamble_freq_rx2=fftshift(fft(preamble_time_rx2,2048)/sqrt(2048));
    % Trim Guard subcarriers
 %   preamble_freq_rx2(1877:2048)=[];  preamble_freq_rx2(1:173)=[];
            % Extract preamble pilots AND Channel Estimation
 %           preamble_pilots_tx1=preamble_freq_rx2(preamble_index_tx1+1);
 %           preamble_pilots_tx2=preamble_freq_rx2(preamble_index_tx2+1);
 %           ChanEst_12=ls_freq(preamble_pilots_tx1,preamble_mod_tx1);
 %           ChanEst_22=ls_freq(preamble_pilots_tx2,preamble_mod_tx2);
            % Interpolate  Note: If interpolate 'Tx1', must remove DC
 %           FullChanEst_12=interpolate_2x2(preamble_index_tx1+1,ChanEst_12,'linear','Tx1');
 %           FullChanEst_22=interpolate_2x2(preamble_index_tx2+1,ChanEst_22,'linear','Tx2');
            
        % ###### MSE Calculation ######
        mse=mse+sum(abs(PwNorm*perfectCE_11(frm,:)-FullChanEst_11).^2)/1703 ...
               +sum(abs(PwNorm*perfectCE_21(frm,:)-FullChanEst_21).^2)/1703 ...
               +sum(abs(PwNorm*perfectCE_12(frm,:)-FullChanEst_12).^2)/1703 ...
               +sum(abs(PwNorm*perfectCE_22(frm,:)-FullChanEst_22).^2)/1703;
           
           % Remove DC
            FullChanEst_11(852)=[];
            FullChanEst_21(852)=[];
            FullChanEst_12(852)=[];
            FullChanEst_22(852)=[];
            
    for sym=1:demparams.sympf  % Spatial Multiplexing
        % ------Rx 1------
        [sym_time, symlen] = fread(ipfid_rx1, 2*2048*(1+demparams.Guard), 'float32');
        if symlen~=2*2048*(1+demparams.Guard), error('fread channel output data error!'); end
        sym_time =  [ 1 1j ] * reshape(sym_time, 2, symlen/2);
        symlen=symlen/2;
        % remove CP
        sym_time=sym_time((2048*demparams.Guard+1):end);
        % FFT
        sym_freq_R1=fftshift(fft(sym_time,2048)/sqrt(2048));     % Rx 1
        % ##### Trim Guard subcarriers #####
             sym_freq_R1(1877:2048)=[];  sym_freq_R1(1:173)=[];
        % Remove DC
             sym_freq_R1(852)=[];
             
        % ------Rx 2------
        [sym_time, symlen] = fread(ipfid_rx2, 2*2048*(1+demparams.Guard), 'float32');
        if symlen~=2*2048*(1+demparams.Guard), error('fread channel output data error!'); end
        sym_time =  [ 1 1j ] * reshape(sym_time, 2, symlen/2);
        symlen=symlen/2;
        % remove CP
        sym_time=sym_time((2048*demparams.Guard+1):end);
        % FFT
        sym_freq_R2=fftshift(fft(sym_time,2048)/sqrt(2048));     % Rx 2
        % ##### Trim Guard subcarriers #####
             sym_freq_R2(1877:2048)=[];  sym_freq_R2(1:173)=[];
        % Remove DC
             sym_freq_R2(852)=[];
        
        % ###### Detection ######
        DetectSymbol_1=zeros(1,length(sym_freq_R1));
        DetectSymbol_2=zeros(1,length(sym_freq_R2));
        for loop=1:length(sym_freq_R1)
            R=[sym_freq_R1(loop),sym_freq_R2(loop)].';
            H=[FullChanEst_11(loop),FullChanEst_21(loop);
               FullChanEst_12(loop),FullChanEst_22(loop)];
            % ###### Sphere Decoder ######
            %[Subsym,erasure]=sphere_decoding(R,H,2,ModScheme);
            %if erasure==1, error('Sphere Decoding failure!');end
            % ############################
            
            % ###### Zero Forcing ######
            Subsym=ZeroForcing_Detection(R,H);
            % ##########################
            
            % ###### MMSE ######
            %Subsym=mmse_Detection(R,H,NoisePwr,2);
            % ##################
            
            DetectSymbol_1(loop)=Subsym(1);
            DetectSymbol_2(loop)=Subsym(2);
            
        end % end loop
        
         % Demap
         DemapSymbol_1=demap(DetectSymbol_1, demparams.qam);
         DemapSymbol_2=demap(DetectSymbol_2, demparams.qam);
         
         % compare with original data for SER
         suberr=suberr+length(find(orig_DFUSC(2*(sym-1)+1,:)-DemapSymbol_1));
         suberr=suberr+length(find(orig_DFUSC(2*(sym-1)+2,:)-DemapSymbol_2));
     end    % end sym
end     % end frm

ser=suberr/datanum
mse=mse/(4*demparams.numframe)